package com.zzy.filter;

import com.zzy.model.FlowStatus;
import com.zzy.service.FlowService;
import com.zzy.util.activiti.UtilFlow;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

@SuppressWarnings("serial")
public class RefuseLinstener implements ExecutionListener {

	@Autowired
	private FlowService flowService;

	public RefuseLinstener() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		ServletContext sc = request.getSession().getServletContext();
		XmlWebApplicationContext cxt = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(sc);
		if(cxt != null && cxt.getBean("flowService") != null){
			this.flowService = (FlowService) cxt.getBean("flowService");
		}
	}


	public void notify(DelegateExecution execution) throws Exception {
		String processInstanceId = execution.getProcessInstanceId();
		FlowStatus actStatus = new FlowStatus();
		actStatus.setProcinsid(processInstanceId);
		actStatus.setStatus(UtilFlow.FLOW_REFUSE);
		flowService.saveActStatus(actStatus);
	}

}
